#include "../main.h"

#define MAX_FDS 10
#define PATH_FIFO "/home/yujiu/fifo"
int main()
{
    int ret=0;

    ret=access(PATH_FIFO,F_OK);
    if(ret==-1){
        perror("access:");
        exit(1);
    }
    int w_fd=open(PATH_FIFO,O_WRONLY);
    if(w_fd==-1){
        perror("open:");
        exit(1);
    }

    struct pollfd fds[MAX_FDS]={0};
    struct pollfd read_fd={
        .fd=0,
        .events=POLLIN,
    };
    struct pollfd write_fd={
        .fd=w_fd,
        .events=POLLOUT
    };
    fds[0]=read_fd;
    fds[1]=write_fd;
    nfds_t nfds=2;
    char buf[128]={0};
    while(1){
        ret=poll(fds,nfds,-1);
        if(ret==-1){
            perror("poll:");
            exit(1);
        }else if(ret==0){
            continue;
        }else{
            for(int i=0 ;i<MAX_FDS;i++){
                if(fds[i].revents==POLLIN){
                    memset(buf,0,sizeof buf);
                    fgets(buf,sizeof(buf),stdin);
                    write(w_fd,buf,strlen(buf));
                    if(strncmp(buf,"exit",4)==0){
                        exit(0);
                    }
                }
            }
        }
    }
return 0;
}
